#pragma once
#include "View.h"
#include "CalcAnimation.h"
#include <memory>
#include "Rect.h"
class SkCanvas;
class SkFont;

class Edit :
	public View
{
public:
	Edit(View* parent);
	virtual ~Edit();
	virtual bool isLockFocus() { return true; }

	virtual void mouseEntry();
	virtual void mouseLeave();
	virtual void gotFocus();
	virtual void loseFocus();

	void appendText(const std::wstring& text);

protected:
	virtual bool paint(SkCanvas* canvas);

	virtual bool paintBackground(SkCanvas* canvas);

	virtual bool paintFrontground(SkCanvas* canvas);

	// virtual bool paintBorder(SkCanvas* canvas);

	virtual bool paintHover(SkCanvas* canvas);

	virtual bool paintFocus(SkCanvas* canvas);

	virtual bool paintCursorBlink(SkCanvas* canvas);
	
	virtual bool processMouseEvent(MouseEvent& event);

	virtual bool processKeyEvent(KeyEvent& event);

	virtual bool processRawKeyEvent(KeyEvent& event);

	virtual bool updateUIPos();
	virtual bool updateStringPos();

	virtual bool deleteChar(bool back_space);

private:
	void goUp();
	void goEnd();
	void goHome();
	void goDown();
	void goLeft();
	void goRight();

	virtual void onChar(wchar_t ch);

private:
	struct EditLine
	{
		std::wstring str;
		int start_line;
		int end_line;
	};

	std::list<std::shared_ptr<EditLine>> text_;
	// CalcAnimation animation_;
	std::shared_ptr<SkFont> font_;

	bool cursor_changed_;
	int cursor_x_;
	int cursor_y_;

	bool ui_cursor_changed_;
	int ui_cursor_xpos_;
	int ui_cursor_ypos_;

	int char_per_line_;
	int lines_;

	int mergin_left_;
	int mergin_top_;
	int mergin_right_;
	int mergin_bottom_;

	// UI pos
	bool select_;
	gfx::Point select_start_;
	gfx::Point select_end_;

	bool auto_scroll_;

	bool lock_scroll_;
	int locked_start_line_;
	int count_ui_line_;
};

